//
//  JMMenuView.m
//  分页控制器
//
//  Created by JackMeng on 16/3/17.
//  Copyright © 2016年 JackMeng. All rights reserved.
//

#import "JMMenuView.h"
#import "UIView+CZFrame.h"

@interface JMMenuView()

@property (weak, nonatomic) IBOutlet UIButton *leftButton;
@property (weak, nonatomic) IBOutlet UIButton *middleButton;
@property (weak, nonatomic) IBOutlet UIButton *rightButton;
@property (weak, nonatomic) IBOutlet UIView *animateView;


@end

@implementation JMMenuView

- (instancetype) init
{
    if (self = [super init])
    {
        self = [[[NSBundle mainBundle] loadNibNamed:@"JMMenuView" owner:nil options:nil] lastObject];
    }
    return self;
}

- (void)awakeFromNib
{
    //默认选中左边按钮
    self.leftButton.selected = YES;
}

- (IBAction)leftButtonClick {
    
    if (_scollView) [_scollView setContentOffset:CGPointZero animated:YES];
}

- (IBAction)middleButtonClick {
    
    if (_scollView) [_scollView setContentOffset:CGPointMake(_scollView.contentSize.width/3.f, 0) animated:YES];
}

- (IBAction)rightButtonClick {
    
    if (_scollView) [_scollView setContentOffset:CGPointMake(_scollView.contentSize.width/3.f * 2, 0) animated:YES];
}
- (void)setWidth:(CGFloat)width
{
    _width = width;
    self.w = width;

}
#pragma mark - 动画
- (void)animateViewProgress:(CGFloat)progress
{
//    NSLog(@"%f",progress);
    //让下面那根线动起来
    if (progress>0 && progress<1) {
        self.animateView.x = progress * self.w;
    }
    //根据偏移 选择按钮
    if (progress > 1/6.f && progress <= 0.5)
    {
        [self selectMiddleButton];
    }
    else if(progress > 0.5)
    {
        [self selectRightButton];
    }
    else
    {
        [self selectLeftButton];
    }
    
}

#pragma mark 切换选中按钮
- (void)selectMiddleButton
{
    if (!self.middleButton.selected)
    {
        self.middleButton.selected = YES;
        self.leftButton.selected = NO;
        self.rightButton.selected = NO;
    }
}

- (void)selectRightButton
{
    if (!self.rightButton.selected)
    {
        self.middleButton.selected = NO;
        self.leftButton.selected = NO;
        self.rightButton.selected = YES;
    }
}

- (void)selectLeftButton
{
    if (!self.leftButton.selected)
    {
        self.middleButton.selected = NO;
        self.leftButton.selected = YES;
        self.rightButton.selected = NO;
    }
}

#pragma mark 设置按钮标题
- (void)setLeftTitle:(NSString *)leftTitle
{
    _leftTitle = leftTitle;
    if (self.leftButton) {
        [self.leftButton setTitle:leftTitle forState:UIControlStateNormal];
        [self.leftButton setTitle:leftTitle forState:UIControlStateHighlighted];
        [self.leftButton setTitle:leftTitle forState:UIControlStateSelected];
    }
}
- (void)setMiddleTitle:(NSString *)middleTitle
{
    _middleTitle = middleTitle;
    if (self.middleTitle) {
        [self.middleButton setTitle:middleTitle forState:UIControlStateNormal];
        [self.middleButton setTitle:middleTitle forState:UIControlStateHighlighted];
        [self.middleButton setTitle:middleTitle forState:UIControlStateSelected];
    }
}
- (void)setRightTitle:(NSString *)rightTitle
{
    _rightTitle = rightTitle;
    if (self.rightButton) {
        [self.rightButton setTitle:rightTitle forState:UIControlStateNormal];
        [self.rightButton setTitle:rightTitle forState:UIControlStateHighlighted];
        [self.rightButton setTitle:rightTitle forState:UIControlStateSelected];
    }
}

@end





